//
//  UICGDirectionsRequest.m
//  MapDirectionsV3
//
//  Created by Kishikawa Katsumi on 09/12/20.
//  Copyright 2009 Kishikawa Katsumi. All rights reserved.
//

#import "UICGDirectionsRequest.h"
#import "UICGDirectionsWaypoint.h"

@implementation UICGDirectionsRequest

@synthesize origin;
@synthesize destination;
@synthesize waypoints;
@synthesize provideTripAlternatives;
@synthesize region;
@synthesize travelMode;
@synthesize unitSystem;

- (id)init {
    if (self = [super init]) {
        waypoints = [[NSArray alloc] init];
        region = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
        travelMode = UICGDirectionsTravelModeDriving;
        unitSystem = UICGDirectionsUnitSystemMetric;
    }
    return self;
}

- (id)initWithStartPoint:(id)start endPoint:(id)end {
    if (self = [self init]) {
        self.origin = start;
        self.destination = end;
    }
    return self;
}

- (void)dealloc {
    [origin release];
    [destination release];
    [waypoints release];
    [region release];
    [super dealloc];
}

- (NSString *)JSONRepresentation {
    NSMutableString *waypointsFragment = [NSMutableString string];
    for (UICGDirectionsWaypoint *waypoint in waypoints) {
        [waypointsFragment appendFormat:@"%@,", [waypoint JSONRepresentation]];
    }
    
    NSString *JSON = [NSString stringWithFormat:
                      @"{'origin':'%@', \
                      'destination':'%@', \
                      'waypoints':[%@], \
                      'provideRouteAlternatives':false, \
                      'region':'%@', \
                      'travelMode':%@, \
                      'unitSystem':google.maps.DirectionsUnitSystem.METRIC}",
                      origin, destination, waypointsFragment, region, 
                      travelMode ? 
                      @"google.maps.DirectionsTravelMode.WALKING" : 
                      @"google.maps.DirectionsTravelMode.DRIVING"];
    return JSON;
}

@end
